<?php
/**
 * Author: 式神(vashi)
 * email: 289650682@qq.com
 * Date: 2019/1/10 11:56
 * FileName: Auth.php
 */

namespace app\http\middleware;


use luck\Jwt;

class Auth
{
    public function handle($request, \Closure $next)
    {
        $token=request()->header('Authorization');
        $jwt=Jwt::checkJwt($token,config('app.crypt.key'));
        if($jwt['status']!=200){
            $res=[
                'code'=>5,
                'msg'=>$jwt['message']
            ];
            return json($res);
        }
        //获取用户数据
        $userdata = Jwt::decodeJwt($token,config('app.crypt.key'))['data'];

        $module_name=strtolower(request()->module().'/'.request()->controller().'/'.request()->action());

        $auth = new \luck\Auth();
        if(!authcheck($module_name,$userdata[config('app.ADMIN_AUTH_KEY')],$auth) ){
            //echo '没有权限';
            if(!request()->isPost()){
                $res['code']=3;
                $res['msg']='没有权限';
                return json($res);
            }else{
                $res['code']=3;
                $res['msg']='你没有权限';
                return json($res);
            }
        }
        /*
        $this->access_token=request()->header('Authorization');

        //获得登录用户
        if(!cache('userdata_'.$this->access_token)){
            //获取用户数据
            $userdata = Jwt::decodeJwt($this->access_token,config('app.crypt.key'))['data'];
            cache('userdata_'.$this->access_token,$userdata);
        }
        $this->userdata=cache('userdata_'.$this->access_token);*/

        $request->userdata=$userdata;

        return $next($request);
    }
}